package com.api.controller

import com.base.db.mysql.service_getUserByAccount
import com.base.db.redis.cache_getCode_pic
import com.base.db.redis.cache_getToken_refresh
import com.base.model.api.APIFrom
import com.yfree.utils.YMD5Utils

class Controller_API_Service private constructor() {
    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { Controller_API_Service() }
    }

    //线路心跳
    fun Controller_API.SERVICE_00A01(params: Map<*, *>) = yResult()

    //刷新sign
    fun Controller_API.SERVICE_01A00(params: Map<*, *>): String {
        val refreshToken = _getRefreshToken()
        if (refreshToken.isNullOrBlank()) return yResult(code = -1, message = "签名不合法")
        val uid = redisService.cache_getToken_refresh(APIFrom.Service, refreshToken)
        if (uid.isNullOrBlank()) return yResult(code = -1, message = "签名过期")
        return yResult(result = listOf(_getUserModelByUID(uid, APIFrom.Service)))
    }

    //登录_账号
    fun Controller_API.SERVICE_01A01(params: Map<*, *>): String {
        val password = params["password"]?.toString()
        val loginname = params["loginname"]?.toString()
        val code = params["code"]?.toString()
        if (password.isNullOrEmpty() || loginname.isNullOrEmpty() || code.isNullOrEmpty()) return yResult(code = -1, message = "数据不合法")
        if (password.length < 6) return yResult(code = -1, message = "密码至少6位")
        if (loginname.length < 6) return yResult(code = -1, message = "用户名至少6位")
        if (redisService.cache_getCode_pic(this, loginname) != code.toLowerCase()) return yResult(code = -1, message = "验证码错误")
        val uid = mysqlService.service_getUserByAccount(loginname, encodePassword(password))
        if (uid.isNullOrEmpty()) return yResult(code = -1, message = "密码错误")
        return yResult(result = listOf(_getUserModelByUID(uid, APIFrom.Service)))
    }
}
